package com.zksk.admin.controller.org;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.domain.org.vo.OrgProductRequestVo;
import com.zksk.admin.domain.org.vo.OrgProductVo;
import com.zksk.admin.service.org.*;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构产品类 信息操作处理
 *
 * @author zksk
 */
@Slf4j
@RestController
@RequestMapping("/org/product")
@Api(value = "OrgProductController", tags = "机构产品相关接口")
public class OrgProductController {

    @Autowired
    private OrgProductService orgProductService;


    @Log(title = "机构产品列表查询", businessType = BusinessType.SELECT)
    @ApiOperation(value = "获取机构产品列表")
    @PostMapping("/list")
    public R<Page> list(@RequestBody OrgProductRequestVo requestVo){
        //构建分页
        Page<OrgProduct> pageInfo = new Page<>(requestVo.getPageNum(), requestVo.getPageSize());
        //构建条件
        LambdaQueryWrapper<OrgProduct> wrapper = new LambdaQueryWrapper<OrgProduct>()
                .like(StringUtils.isNotBlank(requestVo.getEnumProdName()), OrgProduct::getEnumProdName, requestVo.getEnumProdName())
                .eq(StringUtils.isNotBlank(requestVo.getNeedAgreementType()), OrgProduct::getNeedAgreementType, requestVo.getNeedAgreementType())
                .eq(requestVo.getOrgId() != null, OrgProduct::getOrgId, requestVo.getOrgId())
                .orderByAsc(OrgProduct::getCreateTime);

        Page<OrgProduct> page = orgProductService.page(pageInfo, wrapper);
        return R.ok(page);
    }

    @Log(title = "机构产品", businessType = BusinessType.SELECT)
    @GetMapping("/{orgProductId}")
    @ApiOperation(value = "查看机构产品详情")
    public R<OrgProductVo> select(@PathVariable Long orgProductId){
        // 1、查看orgProduct信息
        // 机构产品
        OrgProductVo orgProductVo = orgProductService.selectOrgProductVo(orgProductId);
        if (orgProductVo == null) {
            return R.fail("该产品不存在");
        }
        return R.ok(orgProductVo);
    }
    /**
     * 新增机构产品
     * @param vo
     * @return
     */
    @Log(title = "机构产品服务", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增机构产品服务")
    public R<OrgProduct> add(@Validated(Insert.class) @RequestBody OrgProductVo vo) {
        /**
         * TODO 对机构产品信息中的字段进行判断
         */

        OrgProduct orgProduct = orgProductService.insertOrgProduct(vo);
        return R.ok(orgProduct, "新增机构产品成功");
    }


    // 更新机构产品信息
    @Log(title = "机构产品服务", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "更新机构产品服务")
    public R<OrgProduct> update(@Validated(Update.class) @RequestBody OrgProductVo vo){
        /**
         * TODO 对机构产品信息中的字段进行判断
         */
        Long orgId = vo.getOrgProduct().getId();
        OrgProduct resultOrgProduct = orgProductService.getById(orgId);
        if (orgId == null || orgId <=0 || resultOrgProduct == null){
            return R.fail("更新失败");
        }
        resultOrgProduct = orgProductService.updateOrgProduct(vo);
        return R.ok(resultOrgProduct, "更新成功");
    }

    /**
     * 删除机构产品
     * @param ids
     * @return
     */
    @Log(title = "机构产品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除机构产品(逻辑删除)")
    public R<String> remove(@PathVariable("ids") List<Long> ids){
        boolean flag = orgProductService.deleteOrgProductRelation(ids);
        if (flag){
            // 删除机构产品 删除
            orgProductService.removeBatchByIds(ids);
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }


    /**
     * 查看机构产品信息
     * @param orgProductId
     * @return
     */
    @GetMapping("/getOrgProductById")
    @ApiOperation(value = "查看机构产品")
    public R<OrgProduct> getOrgProductById(@RequestParam Long orgProductId){
        OrgProduct byId = orgProductService.getById(orgProductId);

        return R.ok(byId, "查看详情成功");
    }

}
